<script type="text/javascript" src="<?php echo base_url(); ?>static/select2/select2.min.js" /></script>
<script type="text/javascript" src="<?php echo base_url(); ?>static/js/select.js" /></script>

<form method="post" id="formuser" action="#" enctype="multipart/form-data" class="form-horizontal" role="form" onsubmit="return cekDulu(this);">
	<div class="col-md-offset-1">
		<div class="col-md-11">
			<div class="panel-body well">
				<h4 class="panel-title"><b class="caret"></b><b> Form - Add Contract</b></h4>
				<div class="col-md-6" style="padding-top: 20px;">
					<div class="form-group">
						<label class="col-md-4"><p class="text-left">Vendor Name<span style="color:red;">*</span></p></label>
						<div class="col-md-7">
							<!--select id="vendor_id" class="form-control input-sm required">
								<option value="0">-- select --</option>
							<?php 
								/*
								foreach($vendor_select as $vendor_select => $key)
								{
									echo '<option value="'.$key['VENDOR_ID'].'">'.$key['VENDOR_NAME'].'</option>';
								} 
								*/
							?>							
							</select-->								
							<input type="text" id="vendor_id" class="select2 form-control input-sm"/> 							
						</div>
					</div>
					<div class="form-group">
						<div class="col-md-4"><label for="l_doc_no" class="control-label">Contract No<span style="color:red;">*</span></label></div>
						<div class="col-md-7"><input type="text" class="form-control input-sm required" id="doc_no" value=""></div>
					</div>
					<div class="form-group">
						<div class="col-md-4"><label for="l_doc_title" class="control-label">Contract Title<span style="color:red;">*</span></label></div>
						<div class="col-md-7"><input type="text" class="form-control input-sm required" id="doc_title" maxlength="30"></div>
					</div>
					<div class="form-group">
						<div class="col-md-4"><label class="control-label">Period Date<span style="color:red;">*</span></label></div>
						<div class="col-md-5">
							<div class="input-group">
								<span class="input-group-addon"><small>From</small></span>
								<input type="text" class="form-control input-sm" autocomplete="off" id="date1" value="<?php echo $dt1?>">
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-4"></label>
						<div class="col-md-5">
							<div class="input-group">
								<span class="input-group-addon"><small>To</small></span>
								<input type="text" class="form-control input-sm" autocomplete="off" id="date2" value="<?php echo $dt2?>">
							</div>
						</div>
					</div>
					<!--div class="form-group">
						<div class="col-md-4"><label for="date" class="control-label">Period Date<span style="color:red;">*</span></label></div>
						<div class="col-md-5">
							<div class="input-group">
								<input type="text" class="form-control input-sm required" autocomplete="off" name="date1" id="date1" value="< ?php echo $dt1;?>">
								<span class="input-group-addon" style="cursor:pointer" id="image_button"><span class="glyphicon glyphicon-calendar"></span></span>
							</div>
							<b>s/d</b>
							<div class="input-group">
								<input type="text" class="form-control input-sm required" autocomplete="off" name="date2" id="date2" value="< ?php echo $dt2;?>">
								<span class="input-group-addon" style="cursor:pointer" id="image_button2"><span class="glyphicon glyphicon-calendar"></span></span>
							</div>
						</div>
					</div-->		
					<div class="form-group">
						<div class="col-md-4"><label for="l_price" class="control-label">Price</label></div>
					</div>							
				</div>
				<div class="col-sm-6 col-md-5">
					<div class="form-group">
						<div class="col-md-4"><label for="l_description" class="control-label">Description</label></div>
						<div class="col-md-8">
							<textarea id="description" row="6" cols="50" class="form-control input-sm" maxlength="160"></textarea>
						</div>
					</div>            
					<div class="form-group">
						<div class="col-md-4"><label for="l_upload_file" class="control-label">Upload File</label></div>
						<div class="col-md-7">
							<input type="file" id="userfile" size="20" onchange="myFunction()">
							<input type="hidden" class="form-control input-sm" id="nama_file" value="">
						</div>
					</div>
					
					<script type="text/javascript">
					function myFunction()
					{
						var x = document.getElementById("userfile");
						var y = document.getElementById("nama_file");
						y.value = x.value;
					}
					</script>
					
					<div class="form-group">
						<div class="col-md-4"><label for="l_reminder" class="control-label">Reminder</label></div>
						<div class="col-md-3">
							<input type="text" class="form-control input-sm" id="reminder" value="1">							
						</div>
						<div class="col-md-4"><i>Bulan Sebelumnya</i></div>
					</div>
				</div>
				<div id="tablecontent" class="col-md-12" style="padding-top: 0px;">					
					<div class="form-group">
						<div class="table-responsive">
							<table class="table table-bordered table-condensed table-striped" style="background:#f9f9f9;">
								<thead>
									<tr>
										<th class="tblhead" style="width:3%;text-align:center;">*</th>
										<th class="tblhead" style="width:27%;text-align:center;">Type</th>
										<th class="tblhead" style="width:14%;text-align:center;">Satuan</th>
										<th class="tblhead" style="width:10%;text-align:center;">Qty Awal</th>
										<th class="tblhead" style="width:10%;text-align:center;">Qty Akhir</th>
										<th class="tblhead" style="width:10%;text-align:center;">Currency</th>
										<th class="tblhead" style="width:10%;text-align:center;">Price</th>
										<th class="tblhead" style="width:14%;text-align:center;">Penagihan</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:3%;text-align:center;">+</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:27%;text-align:left;">
											<select id="type" class="form-control input-sm required">
												<option value="0">-- select --</option>
											<?php 
												foreach($type_select as $type_select => $key)
												{
													echo '<option value= '.$key['TYPE_PRICE_ID'].'>'.$key['TYPE_PRICE_DESC'].'</option>';
												};
											?>
											</select>
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:14%;text-align:left;">
											<select id="satuan" class="form-control input-sm required">
												<option value="0">-- select --</option>
											<?php 
												foreach($satuan_select as $satuan_select => $key)
												{
													echo '<option value= '.$key['TYPE_SATUAN_NAME'].'>'.$key['TYPE_SATUAN_NAME'].'</option>';
												};
											?>
											</select>
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<input type="text" class="form-control input-sm required" id="qty_awal" value="0">	
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<input type="text" class="form-control input-sm required" id="qty_akhir" value="0">							
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<select id="currency" class="form-control input-sm">
												<option value="US">US$</option>
												<option value="RP">RP</option>
											</select>
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<input type="text" class="form-control input-sm required" id="price" value="0">							
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:14%;text-align:left;">
											<select id="penagihan" class="form-control input-sm">
												<option value="BULAN">Bulanan</option>
												<option value="TAHUN">Tahunan</option>
											</select>				
										</td>
									</tr>
								</tbody>
							</table>
						</div>
					</div>
				</div>
				<label class="col-md-8"><p class="text-left"><span style="color:red;">* Note: Jika data tabel lebih dari satu, sisanya dimasukkan saat edit data.</span></p></label>
				<div class="col-sm-8" style="padding-top: 20px;">
					<div class="col-sm-4"><label for="button" class="control-label">&nbsp;</label></div>
					<div class="col-sm-4">
						<button type="submit" class="btn btn-success btn-block"><!--span class="glyphicon glyphicon-saved"></span--> Save Add</button>
					</div>
					<div class="col-sm-4">
						<button type="button" class="btn btn-default btn-block" onclick="cancel_exit();">
						<!--span class="glyphicon glyphicon-circle-arrow-left"></span--> Cancel</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</form>

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function() {
            var x = document.getElementById("userfile");
			
			//$tmpname = $_FILES['file']['tmp_name'];
			
			//if (!frm.elements['price'].value=="0") {
				$.post("<?= site_url() ?>setup/insert_data_contract", {
					vendor_id: $('#vendor_id').val(), 
					doc_no: $('#doc_no').val(), 
					doc_title: $('#doc_title').val(), 
					date1: $('#date1').val(), 
					date2: $('#date2').val(), 
					description: $('#description').val(), 
					reminder: $('#reminder').val(), 
					nama_file: $('#nama_file').val(), 
					
					filesize: x.size, 
					filetype: x.type,  
					
					type: $('#type').val(),
					satuan: $('#satuan').val(),
					qty_awal: $('#qty_awal').val(),
					qty_akhir: $('#qty_akhir').val(),
					currency: $('#currency').val(),
					price: $('#price').val(),
					penagihan: $('#penagihan').val()
				},
				function(data) {
					window.location = '<?php echo base_url();?>setup/contract';
				});
			//}
        }
    });

    $(document).ready(function() {
        $("#formuser").keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $.validator.setDefaults({
                    submitHandler: function() {
						var x = document.getElementById("userfile");
						//if (!frm.elements['price'].value=="0") {
							$.post("<?= site_url() ?>setup/insert_data_contract", {
								vendor_id: $('#vendor_id').val(), 
								doc_no: $('#doc_no').val(), 
								doc_title: $('#doc_title').val(), 
								date1: $('#date1').val(), 
								date2: $('#date2').val(), 
								description: $('#description').val(), 
								reminder: $('#reminder').val(), 
								nama_file: $('#nama_file').val(), 
								
								filesize: x.size, 
								filetype: x.type,  
								
								type: $('#type').val(),
								satuan: $('#satuan').val(),
								qty_awal: $('#qty_awal').val(),
								qty_akhir: $('#qty_akhir').val(),
								currency: $('#currency').val(),
								price: $('#price').val(),
								penagihan: $('#penagihan').val()
							},
							function(data) {
								window.location = '<?php echo base_url();?>setup/contract';
							});        
						//} 
                    }
                });
            }
        });
        $("#formuser").validate();
    });

	function cancel_exit() {
		$.post("<?= site_url() ?>setup/contract", function(data) {
            window.location = '<?php echo base_url();?>setup/contract';
        });		
    }
	
	function cekDulu(frm) {
		if (frm.elements['vendor_id'].value=="0") {
			frm.elements['vendor_id'].focus();
			alert('Isi Vendor Name nya');
			return false;
		}
		if (frm.elements['doc_no'].value.length==0) {
			frm.elements['doc_no'].focus();
			alert('Isi Contract No nya');
			return false;
		}
//		if (frm.elements['type'].value=="0") {
//			frm.elements['type'].focus();
//			alert('Isi Type Price nya');
//			return false;
//		}
//		if (frm.elements['price'].value=="0") {
//			frm.elements['price'].focus();
//			alert('Isi Total Price nya');
//			return false;
//		}		
	}
</script>

<script type="text/javascript">
//	$('#image_button').click(function()
//	{
//		$('#date1').datetimepicker('show'); //support hide, show and destroy command
//	});
//	
//	$('#image_button2').click(function()
//	{
//		$('#date2').datetimepicker('show'); 
//	});

	$('#date1').datetimepicker({
		scrollInput:false,
		closeOnDateSelect:true,
		datepicker:true,
		timepicker:false,
		format:'d/m/Y',
		minDate:'-1970/01/01',
		onGenerate:function( ct ){$(this).find('.xdsoft_date.xdsoft_weekend').addClass('xdsoft_disabled');}
	});
	
	$('#date2').datetimepicker({
		scrollInput:false,
		closeOnDateSelect:true,
		datepicker:true,
		timepicker:false,
		format:'d/m/Y',
		minDate:'-1970/01/01',
		onGenerate:function( ct ){$(this).find('.xdsoft_date.xdsoft_weekend').addClass('xdsoft_disabled');}
	});

//    $(document).ready(function() {
//     	$('#vendor_id').select2({
//	  		placeholder: "Select vendor name",
//			allowClear: true
//		});
//    });

</script>